//
//  MBStoryAlbumAndAnchorCell.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBStoryAlbumAndAnchorCell.h"

@interface MBStoryAlbumAndAnchorCell () {
    UIView              *_bgView;
    UIImageView         *_img1;
    UIImageView         *_img2;
    UIImageView         *_img3;
    UILabel             *_lb1;
    UILabel             *_lb2;
    UILabel             *_lb3;
    UIButton            *_bt1;
    UIButton            *_bt2;
    UIButton            *_bt3;
}

@end
@implementation MBStoryAlbumAndAnchorCell

- (void)awakeFromNib {
    // Initialization code
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = [UIColor clearColor];
    self.backgroundView = nil;
    
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    for (int i=0 ;i<3; i++) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectZero];
        imgView.backgroundColor = [UIColor clearColor];
        imgView.layer.cornerRadius = 5.f;
        imgView.layer.masksToBounds = YES;
        [_bgView addSubview:imgView];
        imgView.layer.borderColor = TINK_BG_COLOR.CGColor;//TINK_COLOR.CGColor;
        imgView.layer.borderWidth = 5*PERCEN;
        
        UILabel *lb = [[UILabel alloc]initWithFrame:CGRectZero];
        lb.backgroundColor = [UIColor clearColor];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.textColor = [UIColor blackColor];
        lb.font = [UIFont systemFontOfSize:13.];
        [_bgView addSubview:lb];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.backgroundColor = [UIColor clearColor];
        [bt addTarget:self action:@selector(onSelectItemAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:bt];
        
        if (i == 0) {
            _img1 = imgView;
            _lb1 = lb;
            _bt1 = bt;
        }
        else if (i == 1) {
            _img2 = imgView;
            _lb2 = lb;
            _bt2 = bt;
        }
        else if (i == 2) {
            _img3 = imgView;
            _lb3 = lb;
            _bt3 = bt;
        }
    }

}

- (void)setType:(MBStoryAlbumAndAnchorType)type
{
    _type = type;
    [self setNeedsLayout];
}

- (void)onSelectItemAction:(UIButton *)bt
{
    NSString *index = @"1";
    if (bt == _bt1) {
        index = @"1";
    }
    else if (bt == _bt2) {
        index = @"2";
    }
    else if (bt == _bt3) {
        index = @"3";
    }
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectControlByType:withView:withValue:)]) {
        [_delegate onSelectControlByType:_type withView:self withValue:index];
    }
}

- (void)setAlbumListArr:(NSMutableArray *)albumListArr
{
    if (!_albumListArr) {
        _albumListArr = [[NSMutableArray alloc]init];
    }
    [_albumListArr removeAllObjects];
    [_albumListArr addObjectsFromArray:albumListArr];
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    /*
     热
     {
     AuditionNum = 0;
     CoverPath = "";
     CreateTime = 0;
     FansNum = 0;
     FilePath = "";
     ID = 2;
     PraisedNum = 0;
     Sentiment = 3;
     ShowStatus = 1;
     ShowTitle = 2;
     ShowType = 1;
     UserID = 63;
     } 

     */
    //value
    if (_albumListArr.count == 0) {
        self.hidden = YES;
        return;
    } else {
        self.hidden = NO;
    }
    
    if (self.type == MBStoryAlbumAndAnchorType_Album) {
        for (int i=0; i<_albumListArr.count; i++) {
            NSDictionary *dic = [_albumListArr objectAtIndex:i];
            if ([dic isKindOfClass:[NSDictionary class]]) {
                if (i==0) {
                    [_img1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[dic objectForKey:@"IconPath"]]] placeholderImage:[UIImage imageNamed:@"space_qzx_bg"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        _img1.image = [self getCenterImg:image withImgView:_img1];
                    }];
                    _lb1.text = [dic objectForKey:@"AlbumName"];
                }
                else if (i==1) {
                    [_img2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[dic objectForKey:@"IconPath"]]] placeholderImage:[UIImage imageNamed:@"space_qzx_bg"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        _img2.image = [self getCenterImg:image withImgView:_img2];
                    }];
                    _lb2.text = [dic objectForKey:@"AlbumName"];
                }
                else if (i==2) {
                    [_img3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[dic objectForKey:@"IconPath"]]] placeholderImage:[UIImage imageNamed:@"space_qzx_bg"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        _img3.image = [self getCenterImg:image withImgView:_img3];
                    }];
                    _lb3.text = [dic objectForKey:@"AlbumName"];
                }
            }
        }
        
        if (_albumListArr.count >= 3) {
            _img1.layer.borderColor = TINK_BG_COLOR.CGColor;
            _img2.layer.borderColor = TINK_BG_COLOR.CGColor;
            _img3.layer.borderColor = TINK_BG_COLOR.CGColor;
        }
        else if (_albumListArr.count == 2) {
            _img3.layer.borderColor = [UIColor clearColor].CGColor;
        }
        else if (_albumListArr.count == 1) {
            _img2.layer.borderColor = [UIColor clearColor].CGColor;
            _img3.layer.borderColor = [UIColor clearColor].CGColor;
        }
        
    } else {
        for (int i=0; i<_albumListArr.count; i++) {
            NSDictionary *dic = [_albumListArr objectAtIndex:i];
            if ([dic isKindOfClass:[NSDictionary class]]) {
                if (i==0) {
                    [_img1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[dic objectForKey:@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"space_qzx_bg"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        _img1.image = [self getCenterImg:image withImgView:_img1];
                    }];
                    _lb1.text = [dic objectForKey:@"NickName"];
                }
                else if (i==1) {
                    [_img2 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[dic objectForKey:@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"space_qzx_bg"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        _img2.image = [self getCenterImg:image withImgView:_img2];
                    }];
                    _lb2.text = [dic objectForKey:@"NickName"];
                }
                else if (i==2) {
                    [_img3 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,[dic objectForKey:@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"space_qzx_bg"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                       _img3.image = [self getCenterImg:image withImgView:_img3];
                    }];
                    _lb3.text = [dic objectForKey:@"NickName"];
                }
            }
        }
        
        if (_albumListArr.count >= 3) {
            _img1.layer.borderColor = TINK_BG_COLOR.CGColor;
            _img2.layer.borderColor = TINK_BG_COLOR.CGColor;
            _img3.layer.borderColor = TINK_BG_COLOR.CGColor;
        }
        else if (_albumListArr.count == 2) {
            _img3.layer.borderColor = [UIColor clearColor].CGColor;
        }
        else if (_albumListArr.count == 1) {
            _img2.layer.borderColor = [UIColor clearColor].CGColor;
            _img3.layer.borderColor = [UIColor clearColor].CGColor;
        }
    }
    
    //frame
    float itemWidth = 200*PERCEN;
    float itemHeight = (ISIPAD?284*PERCEN:264*PERCEN);
    float itemleft = (ISIPAD?440*PERCEN*(_orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):(ISPortrait?40*PERCEN:(SCREEN_WIDTH-itemWidth*3)/4.));
    float itemk = (ISIPAD?(SCREEN_WIDTH-itemWidth*3-itemleft*2)/2.:itemleft);
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, itemHeight);
    
    _img1.frame = CGRectMake(itemleft, 30*PERCEN, itemWidth, (ISIPAD?180*PERCEN:160*PERCEN));
    _lb1.frame = CGRectMake(_img1.left, (WIDTH_5S ? _img1.bottom +5: _bgView.height-24*PERCEN-20), _img1.width, 20);
    _bt1.frame = CGRectMake(_img1.left, 0, _img1.width, _bgView.height);
    
    _img2.frame = CGRectMake(_img1.right+itemk, _img1.top, _img1.width, _img1.height);
    _lb2.frame = CGRectMake(_img2.left, _lb1.top, _img2.width, 20);
    _bt2.frame = CGRectMake(_img2.left, 0, _img2.width, _bgView.height);
    
    _img3.frame = CGRectMake(_img2.right+itemk, _img1.top, _img1.width, _img1.height);
    _lb3.frame = CGRectMake(_img3.left, _lb1.top, _img3.width, 20);
    _bt3.frame = CGRectMake(_img3.left, 0, _img3.width, _bgView.height);
}

- (UIImage *)getCenterImg:(UIImage *)image withImgView:(UIImageView *)imgView
{
    CGImageRef imageRef = image.CGImage;
    float imgHeight = 1.0 * image.size.width * imgView.height/imgView.width;
    float rectHeight = imgHeight;
    float top = (image.size.height-imgHeight)/2.;
    CGRect rect;
    if (top > 0) {
        //图片实际高度比UI高度高
        if (imgHeight > image.size.height) {
            rectHeight = image.size.height;
            top = (imgHeight - image.size.height)/2.;
        }
        rect = CGRectMake(0, top, image.size.width, rectHeight);
    }
    else {
        //UI高度比图片实际高度高
        float imgWidth = 1.0 * image.size.height * imgView.width/imgView.height;
        float rectWidth = imgWidth;
        float left = (image.size.width-imgWidth)/2.;
        if (imgWidth > image.size.width) {
            rectWidth = image.size.width;
            left = (imgWidth - image.size.width)/2.;
        }
        rect = CGRectMake(left, 0, rectWidth, image.size.height);
    }
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *rectImg = [UIImage imageWithCGImage:imagePartRef];
    CGImageRelease(imagePartRef);
    return rectImg;
}

@end
